//
//  MyAFNTools.m
//  MyIOSProduct111网络框架的封装
//
//  Created by apple on 2016/11/14.
//  Copyright © 2016年 WangChen. All rights reserved.
//

#import "MyAFNTools.h"

@protocol HTTPProxy <NSObject>
@optional
- (NSURLSessionDataTask *)dataTaskWithHTTPMethod:(NSString *)method
                                       URLString:(NSString *)URLString
                                      parameters:(id)parameters
                                  uploadProgress:(nullable void (^)(NSProgress *uploadProgress)) uploadProgress
                                downloadProgress:(nullable void (^)(NSProgress *downloadProgress)) downloadProgress
                                         success:(void (^)(NSURLSessionDataTask *, id))success
                                         failure:(void (^)(NSURLSessionDataTask *, NSError *))failure;

@end


@interface MyAFNTools ()<HTTPProxy>

@end

@implementation MyAFNTools

+(instancetype)sharedTool{
    
    static id instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [MyAFNTools manager];
    });
    return instance;
    
}


+(instancetype)sharedOwnTool{
    
    static MyAFNTools *ownInstance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        ownInstance = [[self alloc]init];
        ownInstance.responseSerializer.acceptableContentTypes = [ownInstance.responseSerializer.acceptableContentTypes setByAddingObject:@"text/plain"];
        
    });
    return ownInstance;
    
}


//抽出网络访问方法
-(void)requestWithURLStr:(NSString *)urlString method:(MyMethod)method paramter:(id)paramter complete:(void (^)(id, NSError *))complete{
    
    void(^successCallBack)(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) = ^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject){
        complete(responseObject,nil);
    };
    void(^failCallBack)(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) = ^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error){
        complete(nil,error);
    };
    
    
    
//    if(method == GET){
//        [[self GET:urlString parameters:paramter progress:nil success:successCallBack
//         failure:failCallBack] resume];
//    }else{
//        [[self POST:urlString parameters:paramter progress:nil success:successCallBack failure:failCallBack] resume];
//    }
    
    
    NSString *methodStr = (method == GET?@"GET":@"POST");
    
    
    
    [[self dataTaskWithHTTPMethod:methodStr URLString:urlString parameters:paramter uploadProgress:nil downloadProgress:nil success:successCallBack failure:failCallBack]resume];
    
    
    
    
    
}


@end
